#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>

/* 线程非安全函数 */
int *getAddr() {
	static int abc = 100;
	return &abc;
}

void task1(int flags) {
	for (int i = 0; i < 5; ++i) {
		sleep(1);
		printf("abc = %d\n", *getAddr());
	}
	pthread_exit(NULL);
}

void task2(int flags) {
	sleep(2);
	int *p = getAddr();
	*p = 200;
}

struct task {
	int a1;
};

int main() {
	pthread_t tid1, tid2;

	int ret = pthread_create(&tid1, NULL, (void *(*)(void *))task1, (void *)100);
	if (ret ) {
		perror("pthread_create");
		exit(-1);
	}

	ret = pthread_create(&tid2, NULL, (void *(*)(void *))task2, (void *)200);
	if (ret ) {
		perror("pthread_create");
		exit(-1);
	}

	pthread_join(tid1, NULL);
	pthread_join(tid2, NULL);
	printf("main process exit!\n");
	return 0;
}
